<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\Site\Controller\PublicMenu;

use App\Site\Service\Input\PublishMenuInput;
use App\Site\Service\SitePublicMenuService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Mine\Annotation\Auth;
use Mine\Annotation\Permission;
use Mine\Middlewares\CheckModuleMiddleware;
use Mine\MineController;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * 公共菜单控制器
 * Class SitePublicMenuController
 */
#[Controller(prefix: "site/publicMenu"), Auth]
#[Middleware(middleware: CheckModuleMiddleware::class)]
class SitePublicMenuController extends MineController
{
    /**
     * 业务处理服务
     * SitePublicMenuService
     */
    #[Inject]
    protected SitePublicMenuService $service;


    /**
     * 列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[GetMapping("index"), Permission("site:publicMenu, site:publicMenu:index")]
    public function index(): ResponseInterface
    {
        return $this->success($this->service->getTree($this->request->all()));
    }

    /**
     * 发布公共菜单
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping("publish"), Permission("site:publicMenu, site:publicMenu:publish")]
    public function publish(): ResponseInterface
    {
        $input = PublishMenuInput::from($this->request->all());
        $this->service->publish($input);
        return $this->success();
    }

    #[PostMapping("unPublish"), Permission("site:publicMenu, site:publicMenu:unPublish")]
    public function unPublish(): ResponseInterface
    {
        $input = PublishMenuInput::from($this->request->all());
        $this->service->unPublish($input);
        return $this->success();
    }
}